﻿using UnityEngine;

public class ScaleSize : NodeSize
{
    public RectTransform rectTransform;
    public Vector3 min;
    public Vector3 max;

    protected override void OnRefresh(float rw, float rh, float w, float h, float s)
    {
        SetRatio(GetRatio(rw, rh, w, h));
    }

    private void SetRatio(float value)
    {   
        rectTransform.localScale = Vector3.Lerp(min, max, value);
    }

    protected float GetRatio(float rw, float rh, float w, float h)
    {
        var v1 = rw / rh;
        var v2 = rh / rw;
        var v = w / h;
        if (v < v1) return 0;
        if (v > v2) return 1;
        return (v - v1) / (v2 - v1);
    }
}